//
//  BaseQuotationStockListController.m
//  Stock
//
//  Created by blacksun on 11-7-4.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "BaseQuotationStockListController.h"
#import "MyStockListController.h"


@implementation BaseQuotationStockListController
@synthesize tableView;
@synthesize quotationCenterService;
@synthesize quotationStocks;
@synthesize key;
@synthesize loadMoreView;

#pragma mark -Service Delegate
-(void)requestFinishedWithResult:(id)aResult andUserinfo:(NSDictionary *)aUserinfo{
    [self.quotationStocks addObjectsFromArray:aResult];
    if (aResult && [aResult count]>1) {
        currentPage++;
    }
    [self.tableView reloadData];
    if (aResult && [aResult count]==20) {
        self.tableView.tableFooterView = self.loadMoreView;
    }else
        self.tableView.tableFooterView = nil;
}
-(void)requestFinishedwithError:(NSError *)aError andUserinfo:(NSDictionary *)aUserinfo{
    
}

-(void)queryStocks{
    self.tableView.tableFooterView = [[StockAppHelper shareInstance] getTableFooterView];
}

- (void)dealloc
{
    
    self.tableView = nil;
    self.key = nil;
    [self.quotationCenterService  cancleAllRequest];
    self.quotationCenterService = nil;
    self.quotationStocks = nil;
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.quotationCenterService = [[[QuotationCenter alloc] initWithDelegate:self] autorelease];
    self.quotationStocks = [NSMutableArray array];
    currentPage = 0;
    
    self.loadMoreView = [[StockAppHelper shareInstance] setupLoadMoreViewWithTarget:self andAction:@selector(queryStocks)];
    
    self.tableView.tableFooterView = [[StockAppHelper shareInstance] getTableFooterView];
    
    if (!IsPaidVersion) {
        ADBannerView *banner = [[ADBannerView alloc] initWithFrame:CGRectMake(0.0, 326.0, 320.0, 50.0)];
        banner.delegate = self;
        [self.view insertSubview:banner belowSubview:self.tableView];
        [banner release];
        NSLog(@"ADBannerView");
    }
    
    [self queryStocks];
    
}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner{
    NSLog(@"bannerViewDidLoadAd");
    CGRect frame = self.tableView.frame;
    frame.size.height = frame.size.height - banner.bounds.size.height;
    //[UIView beginAnimations:nil context:nil];
    //[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    //[UIView setAnimationDuration:0.5];
    tableView.frame = frame;
    //[UIView commitAnimations];
    
    [self.view bringSubviewToFront:banner];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.tableView = nil;
    [self.quotationCenterService  cancleAllRequest];
    self.quotationCenterService = nil;
    self.quotationStocks = nil;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [quotationStocks count];
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        
        UIColor *clearColor = [UIColor clearColor];
        
        UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(4.0, 12.0, 70.0, 20.0)];
        nameLabel.backgroundColor = clearColor;
        nameLabel.textColor = [UIColor whiteColor];
        nameLabel.tag = kStockNameTag;
        UILabel *todayPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(80.0, 12.0, 60.0, 20.0)];
        todayPriceLabel.backgroundColor = clearColor;
        todayPriceLabel.tag = kStockTodayPriceTag;
        todayPriceLabel.textAlignment = UITextAlignmentRight;
        
        UILabel *changeRateLabel = [[UILabel alloc] initWithFrame:CGRectMake(162.0, 12.0, 60.0, 20.0)];
        changeRateLabel.backgroundColor = clearColor;
        changeRateLabel.tag = kStockChangeRateTag;
        changeRateLabel.textAlignment = UITextAlignmentRight;
        
        UILabel *fNetLabel = [[UILabel alloc] initWithFrame:CGRectMake(230.0, 12.0, 85.0, 20.0)];
        fNetLabel.backgroundColor = clearColor;
        fNetLabel.tag = kStockChangePriceTag;
        fNetLabel.textAlignment = UITextAlignmentRight;
        
        
        [cell.contentView addSubview:nameLabel];
        [cell.contentView addSubview:todayPriceLabel];
        [cell.contentView addSubview:changeRateLabel];
        [cell.contentView addSubview:fNetLabel];
        
        [nameLabel release];
        [todayPriceLabel release];
        [changeRateLabel release];
        [fNetLabel release];
        
    }
    NSInteger index = [indexPath row];
    NSString *imageName = @"cellbg2.png";
    if (index%2==1) {
        imageName = @"cellbg1.png";
    }
    cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]] autorelease];
    QuotationStock *currentStock = [quotationStocks objectAtIndex:index];
    UILabel *nameLabel = (UILabel *)[cell viewWithTag:kStockNameTag];
    UILabel *todyLabel = (UILabel *)[cell viewWithTag:kStockTodayPriceTag];
    UILabel *fNetLabel = (UILabel *)[cell viewWithTag:kStockChangePriceTag];
    UILabel *rateLabel = (UILabel *)[cell viewWithTag:kStockChangeRateTag];
    
        
    todyLabel.textColor = [UIColor whiteColor];
    rateLabel.textColor = [currentStock.changedRate hasPrefix:@"-"]?[UIColor greenColor]:[UIColor redColor];
    fNetLabel.textColor = [currentStock.fNet hasPrefix:@"-"]?[UIColor greenColor]:[UIColor redColor];
    
    nameLabel.text = currentStock.name;
    if (currentStock.closingPrice && currentStock.closingPrice != 0) {
        todyLabel.text = [currentStock.closingPrice stringValue];
        rateLabel.text = currentStock.changedRate; 
        fNetLabel.text = currentStock.fNet;
    }else{
        todyLabel.text = @"--";
        rateLabel.text = @"--";  
        fNetLabel.text = @"--";
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    QuotationStock *currentStock = [quotationStocks objectAtIndex:indexPath.row];
    
    [StockContext shareInstance].remoteMyStocks = [NSMutableArray arrayWithObject:currentStock];
    
    StockListController *stockListController = [[StockListController alloc] initWithNibName:@"StockListController" bundle:nil];
    stockListController.myStock = currentStock;
    StockAppDelegate *delegate = [UIApplication sharedApplication].delegate;
    [delegate.navigationController pushViewController:stockListController animated:YES];
    [stockListController release];
}

@end
